using System.Collections;
using UnityEngine;

public class EnemyWatcher : MonoBehaviour
{
	public LayerMask collisionLayer;

	public GameObject lookTarget;

	public TypeHit curTypeHit;

	public PlayerBehavior pBehavior;

	private bool photonIsMine;

	private TPSCamera camWalkPlayer;

	public Ray rayAim;

	public RaycastHit hitAim;

	public Mortal mortalLookTarget;

	private Vector3 posScreenAim;

	private void Start()
	{
		pBehavior = NGUITools.FindInParents<PlayerBehavior>(base.transform);
		if (pBehavior != null && !settings.offlineMode)
		{
			photonIsMine = pBehavior.photonView.isMine;
			if (!settings.offlineMode && !photonIsMine)
			{
				base.enabled = false;
			}
		}
		camWalkPlayer = GameController.instance.tpsCamera;
	}

	private void OnEnable()
	{
		if (base.enabled)
		{
			StartCoroutine(UpdateEnemyList());
		}
	}

	private void OnDisable()
	{
		StopCoroutine(UpdateEnemyList());
	}

	private IEnumerator UpdateEnemyList()
	{
		while (true)
		{
			yield return new WaitForSeconds(0.1f);
			if (WalkWindow.instance == null || WalkWindow.instance.aimHUD == null || UICamera.currentCamera == null)
			{
				yield return null;
				continue;
			}
			posScreenAim = UICamera.currentCamera.WorldToScreenPoint(WalkWindow.instance.aimHUD.transform.position);
			if (!(camWalkPlayer != null) || !(camWalkPlayer.mainCamera != null))
			{
				continue;
			}
			rayAim = camWalkPlayer.mainCamera.ScreenPointToRay(posScreenAim);
			if (!Physics.Raycast(rayAim, out hitAim, 100000f, collisionLayer))
			{
				GameController.instance.aimHit = rayAim.origin + rayAim.direction * 100000f;
				lookTarget = null;
				yield return null;
				continue;
			}
			GameController.instance.aimHit = hitAim.point;
			mortalLookTarget = hitAim.collider.transform.GetComponentInParent<Mortal>();
			if (mortalLookTarget != null && Vector3.Distance(mortalLookTarget.transform.position, pBehavior.transform.position) < 60f)
			{
				lookTarget = mortalLookTarget.gameObject;
			}
			else
			{
				lookTarget = null;
			}
		}
	}

	public Ray FromCenterScreenToAimHUD()
	{
		Vector3 position = UICamera.currentCamera.WorldToScreenPoint(WalkWindow.instance.aimHUD.transform.position);
		Ray result = default(Ray);
		if (camWalkPlayer != null && camWalkPlayer.mainCamera != null)
		{
			return camWalkPlayer.mainCamera.ScreenPointToRay(position);
		}
		BCWDebug.Log("Shoot target detection error.");
		return result;
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.yellow;
	}
}
